package dao;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.slf4j.Logger;


/**
 * Data access object (DAO) for domain model
 * @author MyEclipse Persistence Tools
 */
public class BaseHibernateDAO implements IBaseHibernateDAO {
	public Session getSession() {
		return HibernateSessionFactory.getSession();
	}
	
	protected boolean save(Logger log, String logMessage, Object objecT){
	    log.debug(logMessage);
		try {
		    Session session = getSession();
		    Transaction transaction= session.beginTransaction();
		    session.save(objecT);
		    transaction.commit();
		    log.debug("save successful");
		    return true;
		} catch (RuntimeException re) {		    
		    log.error("save failed", re);
		    throw re;
		    
		}
	}
	
	protected boolean delete(Logger log, String logMessage, Object objecT){
	    log.debug(logMessage);
		try {
		    Session session = getSession();
		    Transaction transaction= session.beginTransaction();
		    session.delete(objecT);
		    transaction.commit();
		    log.debug("delete successful");
		    return true;
		} catch (RuntimeException re) {
		    log.error("delete failed", re);
		    throw re;
		}
	}
	
	
	protected boolean attachDirty(Logger log, String logMessage, Object objectT) {
		log.debug(logMessage);
		try {
		    Session session = getSession();
		    Transaction transaction= session.beginTransaction();
		    session.saveOrUpdate(objectT);
		    transaction.commit();
		    log.debug("attach successful");
		    return true;
		} catch (RuntimeException re) {
		    log.error("attach failed", re);
		    throw re;
		}
	    }
	
	protected boolean update2(Logger log, String logMessage, Object objectT) {
		log.debug(logMessage);
		try {		    
		    Session session = getSession();
		    session.clear();
		    Transaction transaction= session.beginTransaction();
		    session.update(objectT);
		    transaction.commit();
		    log.debug("update successful");
		    return true;
		} catch (RuntimeException re) {
		    log.error("update failed", re);
		    throw re;
		}
	    }
	
	
	protected boolean update(Logger log, String logMessage, Object objectT) {
		log.debug(logMessage);
		try {		    
		    Session session = getSession();
		    Transaction transaction= session.beginTransaction();
		    session.merge(objectT);
		    transaction.commit();
		    log.debug("update successful");
		    return true;
		} catch (RuntimeException re) {
		    log.error("update failed", re);
		    throw re;
		}
	    }
	
}